//
//  SPBluetoothlistPopView.m
//  Spinning
//
//  Created by Neil on 2018/1/26.
//  Copyright © 2018年 com. All rights reserved.
//

#import "DeviceListView.h"
#import "BluetoothListCell.h"

NSString *cellIdentifier1 = @"bluetoothListCellIdentifier";
@interface DeviceListView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIButton *btnCancel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *scanBtn;
@property (nonatomic, strong) NSArray *deviceArray;


@end

@implementation DeviceListView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.deviceArray = @[@"物联网燃气表",@"IC卡无线表"];
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    self.titleLabel = [UILabel labelWithframe:CGRectZero text:@"请选择表类型" font:[UIFont systemFontOfSize:17] textAlignment:NSTextAlignmentCenter textColor:@"#EF9B39"];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.titleLabel];
    
    //    self.scanBtn = [[UIButton alloc] init];
    //    [self.scanBtn setImage:[UIImage imageNamed:@"扫码"] forState:UIControlStateNormal];
    //    [self addSubview:self.scanBtn];
    //    [self.scanBtn addTarget:self action:@selector(scanPeriphealAction) forControlEvents:UIControlEventTouchUpInside];
    
    _listTableView = [[UITableView alloc] init];
    [self addSubview:_listTableView];
    //    [_listTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
    [_listTableView registerClass:[BluetoothListCell class] forCellReuseIdentifier:cellIdentifier1];
    
    _listTableView.rowHeight = 44.0f;
    _listTableView.delegate = self;
    _listTableView.dataSource = self;
    
    
    self.btnCancel = [UIButton buttonWithframe:CGRectZero text:@"取消" font:[UIFont systemFontOfSize:17] textColor:@"#EF9B39" backgroundColor:nil normal:nil selected:nil];
    [self.btnCancel addTarget:self action:@selector(actionHide) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.btnCancel];
    
    
    //    CGFloat alertWidth = 280.0f * scaleX;
    //    CGFloat alertHeight = 256.0f * scaleX;
    //
    //    self.frame = CGRectMake(0, 0, alertWidth, alertHeight);
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 5.0f;
    self.layer.masksToBounds = YES;
    
    
    self.titleLabel.frame = CGRectMake(0, 0, self.width, 40);
    
    
    //    [self.scanBtn makeConstraints:^(MASConstraintMaker *make) {
    //        make.right.equalTo(self.mas_right).offset(-10);
    //        make.centerY.equalTo(self.titleLabel.centerY);
    //        make.height.equalTo(40);
    //        make.width.equalTo(40);
    //    }];
    
    self.btnCancel.frame = CGRectMake(0, self.height-40, self.width, 40);
    
    self.listTableView.frame = CGRectMake(0, self.titleLabel.bottom, self.width, self.btnCancel.top-self.titleLabel.bottom);
    
    
}

- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)hide
{
    [self removeFromSuperview];
}

#pragma mark - table view delegate and datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.deviceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    //    CBPeripheral *peripheral =  (CBPeripheral *)self.periphealArray[indexPath.row];
    //    cell.textLabel.text = peripheral.name;
    //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BluetoothListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1 forIndexPath:indexPath];
    NSString *name = self.deviceArray[indexPath.row];
    cell.nameLabel.text = name;
    cell.row = indexPath.row;
//    cell.delegate = self;
    cell.breakBtn.hidden = YES;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(selectDevice:)]) {
        [self.delegate selectDevice:indexPath.row];
    }
}

#pragma mark - button method
- (void)actionHide {
    [self hide];
}

@end
